﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using SHDocVw;
using System.Windows.Forms;

namespace AgentBar.Common
{
    public class CommonFunction
    {

        public static bool IEOpenOnURL(string reqUrl, string param, int width, int height, bool hasStatus)
        {
            InternetExplorer oIE = new InternetExplorerClass();
            if (oIE != null)
            {
                oIE.Width = width;
                //oIE.MenuBar = hasMenu;
                //oIE.LocationName = "center";
                oIE.Height = height;
                oIE.StatusBar = hasStatus;
                object oEmpty = String.Empty;
                object oURL = reqUrl;
                object bs = Encoding.ASCII.GetBytes(param);
                object head = "Content-Type: application/x-www-form-urlencoded ";
                oIE.Visible = true;
                oIE.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref bs, ref head);
            }
            return true;
        }


        public static void WriteLog(string msg)
        {
            try
            {

                Microsoft.VisualBasic.Logging.Log log = new Microsoft.VisualBasic.Logging.Log();



                log.DefaultFileLogWriter.Location = Microsoft.VisualBasic.Logging.LogFileLocation.Custom;
                //log.DefaultFileLogWriter.CustomLocation = "log_vb";
                //log.DefaultFileLogWriter.BaseFileName = "AgentStatusMonitor" + DateTime.Now.ToString("yyyyMMdd");

                //add,xzg,2011/09/12,S
                string strFile = "";

                //strFile = log.DefaultFileLogWriter.FullLogFileName;
                //strFile = log.DefaultFileLogWriter.FullLogFileName.Replace(log.DefaultFileLogWriter.BaseFileName +".log"
                //                                , "AgentStatusMonitor" + DateTime.Now.DayOfWeek + ".log");

                strFile = log.DefaultFileLogWriter.CustomLocation + "\\" + "AgentBar" + DateTime.Now.DayOfWeek + ".log";

                if (System.IO.File.Exists(strFile))
                {

                    if (System.IO.File.GetLastWriteTime(strFile).ToString("yyyyMMdd") == DateTime.Now.ToString("yyyyMMdd"))
                    {

                    }
                    else
                    {
                        System.IO.File.Delete(strFile);

                    }
                }
                //add,xzg,2011/09/12,E

                log.DefaultFileLogWriter.BaseFileName = "AgentBar" + DateTime.Now.DayOfWeek;




                string strMsg = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                strMsg = strMsg + "    " + msg;
                //log.WriteEntry(strMsg);
                log.DefaultFileLogWriter.WriteLine(strMsg);
                //log.TraceSource.Flush();
                log.DefaultFileLogWriter.Flush();
                log.DefaultFileLogWriter.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
